package garage

import (
	"fmt"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/garage"
	garageReq "github.com/flipped-aurora/gin-vue-admin/server/model/garage/request"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type GarageApi struct {
}

var garageService = service.ServiceGroupApp.GarageServiceGroup.GarageService

// CreateGarage 创建garage表
// @Tags Garage
// @Summary 创建garage表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body garage.Garage true "创建garage表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /garage/createGarage [post]
func (garageApi *GarageApi) CreateGarage(c *gin.Context) {

	var garage garage.Garage
	err := c.ShouldBindJSON(&garage)
	fmt.Println(garage)
	fmt.Println("1111")
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := garageService.CreateGarage(&garage); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteGarage 删除garage表
// @Tags Garage
// @Summary 删除garage表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body garage.Garage true "删除garage表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /garage/deleteGarage [delete]
func (garageApi *GarageApi) DeleteGarage(c *gin.Context) {
	var garage garage.Garage
	err := c.ShouldBindJSON(&garage)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := garageService.DeleteGarage(garage); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteGarageByIds 批量删除garage表
// @Tags Garage
// @Summary 批量删除garage表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除garage表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /garage/deleteGarageByIds [delete]
func (garageApi *GarageApi) DeleteGarageByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := garageService.DeleteGarageByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateGarage 更新garage表
// @Tags Garage
// @Summary 更新garage表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body garage.Garage true "更新garage表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /garage/updateGarage [put]
func (garageApi *GarageApi) UpdateGarage(c *gin.Context) {
	var garage garage.Garage
	err := c.ShouldBindJSON(&garage)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := garageService.UpdateGarage(garage); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindGarage 用id查询garage表
// @Tags Garage
// @Summary 用id查询garage表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query garage.Garage true "用id查询garage表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /garage/findGarage [get]
func (garageApi *GarageApi) FindGarage(c *gin.Context) {
	var garage garage.Garage
	err := c.ShouldBindQuery(&garage)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if regarage, err := garageService.GetGarage(garage.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"regarage": regarage}, c)
	}
}

// GetGarageList 分页获取garage表列表
// @Tags Garage
// @Summary 分页获取garage表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query garageReq.GarageSearch true "分页获取garage表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /garage/getGarageList [get]
func (garageApi *GarageApi) GetGarageList(c *gin.Context) {
	var pageInfo garageReq.GarageSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := garageService.GetGarageInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

func (garageApi *GarageApi) GetGarageListNoPage(c *gin.Context) {

	if list, err := garageService.GetGarageInfoListNoPage(); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List: list,
		}, "获取成功", c)
	}
}
